/*
 * =====================================================================================
 *
 *       Filename:  LinkedQueue.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  11/11/2012 05:17:17 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Kjeldahl (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include  "LinkedQueue.h"

LinkedQueue *initLinkedQueue(){
	LinkedQueue *Q=malloc(sizeof(LinkedQueue));
	Q->front=Q->rear=NULL;
	Q->length=0;
	return Q;

}
Linkeddata * createdata(){
	
	Linkeddata *data=malloc(sizeof(Linkeddata));
	data->pior=data->next=NULL;
	return data;

}
void emptyLinkedQueue(LinkedQueue *Q){
	
	while(Q->front!=NULL){
		pop(Q);
		Q->length--;
	}

}
void destroyLinkedQueue(LinkedQueue *Q){
	emptyLinkedQueue(Q);
	free(Q);

}
Linkeddata * pop(LinkedQueue *Q){
	if(isempty(Q))	{
		printf("The Queue is empty!\n");
	
	}else{	
		Linkeddata *data=Q->front;
		if(data->next==NULL){
			Q->rear=Q->front=NULL;
			Q->length=0;
			printf("The Queue is empty!\n");

		}else{
			Q->front=data->next;
			Q->length--;
			Q->front->pior=NULL;
		}
		return data;
	
	}

}
void  unshift(LinkedQueue *Q,elmType value){
	
	Linkeddata *data=createdata();
	data->info[0]=value;

	if(!isempty(Q)){
		Q->rear->next=data;
		data->pior=Q->rear;
		Q->rear=data;
	}else{
		
		Q->front=Q->rear=data;
	}
	Q->length++;

}
bool isempty(LinkedQueue *Q){

	if(getLength(Q)==0 ) {
		return true;
	}else{
		return false;
	}

}
int getLength(LinkedQueue *Q){

	return Q->length;

}
Linkeddata *getFront(LinkedQueue *Q){
	
	return Q->front;

}
Linkeddata *getRear(LinkedQueue *Q){
	
	return Q->rear;
}
void showQueue(LinkedQueue *Q){
	Linkeddata *data=NULL;
	data=Q->rear;
	if(data==NULL){
		printf("The Queue is empty!\n");
	}else{	
		printf("\nrear->");
	while(data!=NULL){
		
		printf("%d->",data->info[0]);
		data=data->pior;

		}
	printf("front\n");
	}

}
